package com.ht.WeNas.data.dto;

import com.ht.WeNas.utils.MyPathUtil;
import lombok.Data;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;

@Data
public class FileObj {
    String name;
    String type;
    String contentType;
    String path;
    boolean dir;
    boolean hidden;
    long lastUpdateTime;
    long size;

    public static FileObj getInstance(Path path, BasicFileAttributes attrs) {
        FileObj fileObj = new FileObj();
        try {
            fileObj.name = Optional.ofNullable(path.getFileName()).orElse(path).toString();
            fileObj.path = MyPathUtil.toPathString(path);
            fileObj.dir = attrs.isDirectory();
            fileObj.hidden = Files.isHidden(path);
            fileObj.lastUpdateTime = attrs.lastModifiedTime().toMillis();
            fileObj.size = attrs.size();

            // 设置类型和contentType（这里简化处理）
            if (fileObj.dir) {
                fileObj.type = "dir";
                fileObj.contentType = "inode/directory";
            } else {
                String string = path.getFileName().toString();
                fileObj.type = string.substring(string.lastIndexOf('.') + 1);
                fileObj.contentType = Files.probeContentType(path);
            }
        } catch (IOException ignore) {
            return null;
        }
        return fileObj;
    }
    
    public static FileObj getInstance(Path path) {
        try {
            return getInstance(path, Files.readAttributes(path, BasicFileAttributes.class));
        } catch (IOException ignore) {
            return null;
        }
    }
    
    private FileObj() {}
    
    public static Comparator<FileObj> getComparator() {
        return getComparator(new String[]{"name"});
    }
    
    public static Comparator<FileObj> getComparator(String[] compareField) {
        if (compareField == null || compareField.length == 0) {
            compareField = new String[]{"name"};
        }
        final String[] finalCompareField = compareField;
        return (FileObj o1, FileObj o2) -> {
            for (String field : finalCompareField) {
                switch (field.toLowerCase()) {
                    case "name":
                        int nameCompare = o1.getName().compareTo(o2.getName());
                        if (nameCompare != 0) return nameCompare;
                        break;
                    case "type":
                        int contentTypeCompare = Objects.compare(o1.getType(), o2.getType(), Comparator.nullsLast(String::compareTo));
                        if (contentTypeCompare != 0) return contentTypeCompare;
                        break;
                    case "lastupdatetime":
                        long lastUpdateTimeCompare = Long.compare(o1.getLastUpdateTime(), o2.getLastUpdateTime());
                        if (lastUpdateTimeCompare != 0) return (int) lastUpdateTimeCompare;
                        break;
                    case "size":
                        long sizeCompare = Long.compare(o1.getSize(), o2.getSize());
                        if (sizeCompare != 0) return (int) sizeCompare;
                        break;
                    default:
                        return 0;
                }
            }
            return 0;
        };
    }

}
